package com.icatw.club.controller;

import com.icatw.club.domain.ClubMember;
import com.icatw.club.service.IClubCollegeService;
import com.icatw.club.service.IClubMemberService;
import com.icatw.common.annotation.Log;
import com.icatw.common.core.controller.BaseController;
import com.icatw.common.core.domain.AjaxResult;
import com.icatw.common.core.domain.entity.SysUser;
import com.icatw.common.core.page.TableDataInfo;
import com.icatw.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 社团会员Controller
 *
 * @author icatw
 * @date 2022-06-09
 */
@RestController
@RequestMapping("/club/member")
@Api(tags = "社团会员模块")
@Slf4j
public class ClubMemberController extends BaseController {
    @Autowired
    private IClubMemberService clubMemberService;
    @Autowired
    IClubCollegeService clubCollegeService;

    /**
     * 查询社团会员列表
     */
    @PreAuthorize("@ss.hasPermi('club:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClubMember clubMember) {
        startPage();
        List<ClubMember> list = clubMemberService.selectMemberList(clubMember);
        return getDataTable(list);
    }

    /**
     * 导出社团会员列表
     */
    @ApiOperation("导出社团会员列表")
    @PreAuthorize("@ss.hasPermi('club:member:export')")
    @Log(title = "社团会员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ModelMap map,
                       HttpServletRequest request,
                       HttpServletResponse response) {
        clubMemberService.export(map, request, response);
    }

    /**
     * 导入成员列表
     *
     * @param file 文件
     * @return {@link List}<{@link SysUser}>
     * @throws Exception 异常
     */
    @ApiOperation("从Excel导入会员列表")
    @PreAuthorize("@ss.hasPermi('club:member:import')")
    @Log(title = "社团会员", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importMemberList(@RequestPart("file") MultipartFile file) throws Exception {
        return toAjax(clubMemberService.importExcel(file));
    }

    /**
     * 获取社团会员详细信息
     */
    @PreAuthorize("@ss.hasPermi('club:member:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(clubMemberService.getById(id));
    }

    /**
     * 新增社团会员
     */
    @PreAuthorize("@ss.hasPermi('club:member:add')")
    @Log(title = "社团会员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClubMember clubMember) {
        clubMember.setCreateDate(LocalDate.now());
        return toAjax(clubMemberService.save(clubMember));
    }

    /**
     * 修改社团会员
     */
    @PreAuthorize("@ss.hasPermi('club:member:edit')")
    @Log(title = "社团会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClubMember clubMember) {
        clubMember.setUpdateDate(LocalDate.now());
        return toAjax(clubMemberService.updateById(clubMember));
    }

    /**
     * 删除社团会员
     */
    @PreAuthorize("@ss.hasPermi('club:member:remove')")
    @Log(title = "社团会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clubMemberService.removeByIds(Arrays.asList(ids)));
    }
}
